package cn.haoxsf.app.common.util;

import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: app
 * @description: 提供字符串，集合，数组，map等常见对象判空处理
 * @author: Ele qvbanfm@qq.com
 * @create: 2020-01-07 09:50
 **/
public class ToolUtil {
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            String str = (String) obj;
            return "".equals(str.trim());
        }
        if (obj instanceof Number) {
            Number num = (Number) obj;
            return num.byteValue() == 0;
        }
        if (obj instanceof Collection) {
            Collection col = (Collection) obj;
            return col.isEmpty();
        }
        if (obj instanceof Map) {
            Map map = (Map) obj;
            return map.isEmpty();
        }
        if (obj.getClass().getSimpleName().endsWith("[]")) {
            List<Object> list = Arrays.asList(obj);
            Object[] objs = (Object[]) list.get(0);
            return objs.length == 0;
        }
        return false;
    }

    /**
     * 提供字符串，集合，数组，map等常见对象判空处理
     *
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 将对象装换为map
     *
     * @param bean
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 将map装换为javabean对象
     *
     * @param map
     * @param bean
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map, T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }

    /**
     * 以逗号为分割符拼接字符串
     * 直接到最后使用deleteCharAt方法去除最后一个逗号即可
     */
    public static String getStr(List<String> list) {
        StringBuffer sb = new StringBuffer();
        if (ToolUtil.isNotEmpty(list)) {
            for (String str : list) {
                sb.append(str).append(",");
            }
            return sb.deleteCharAt(sb.length() - 1).toString();
        }else {
            return null;
        }

    }

    public static boolean isOneEmpty(Object... os) {
        Object[] var1 = os;
        int var2 = os.length;

        for (int var3 = 0; var3 < var2; ++var3) {
            Object o = var1[var3];
            if (isEmpty(o)) {
                return true;
            }
        }

        return false;

    }

    /**
     * 获取随机位数的数字符串
     *
     * @author fengshuonan
     * @Date 2017/8/24 14:09
     */
    public static String getRandomNumber(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
